import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

//检查是否加固
public class GetReinforcementMoth {

    public static void main(String[] args) throws IOException {
        initData();
        String apkSourceCodeDir[] = {"/home/stephen/Downloads/libs"};
        getReinforcementMothByPaths(apkSourceCodeDir);
    }

    /**
     * 加固so集合
     */
    static HashMap<String, String> reinforcementSoMaps;

    private static void initData() {
        reinforcementSoMaps = new HashMap<String, String>();
        reinforcementSoMaps.put("libsecexe.so", "梆梆加固免费版");
        reinforcementSoMaps.put("libsecmain.so", "梆梆加固免费版");
        reinforcementSoMaps.put("libSecShell.so", "梆梆加固免费版");
        reinforcementSoMaps.put("secData0.jar", "梆梆加固免费版");
        reinforcementSoMaps.put("libSecShell-x86.so", "梆梆加固免费版");

        reinforcementSoMaps.put("libDexHelper.so", "梆梆企业版");
        reinforcementSoMaps.put("libDexHelper-x86.so", "梆梆企业版");
        reinforcementSoMaps.put("classes.jar", "梆梆加固定制版");
        reinforcementSoMaps.put("DexHelper.so", "梆梆加固定制版");

        reinforcementSoMaps.put("libtup.so", "腾讯加固");
//        ReinforcementSoMaps.put("libexec.so", "腾讯加固");
        reinforcementSoMaps.put("libshell.so", "腾讯加固");
        reinforcementSoMaps.put("mix.dex", "腾讯加固");
        reinforcementSoMaps.put("mixz.dex", "腾讯加固");
        reinforcementSoMaps.put("libshella-xxxx.so", "腾讯加固");
        reinforcementSoMaps.put("libshellx-xxxx.so", "腾讯加固");

        reinforcementSoMaps.put("libtosprotection.armeabi-v7a.so", "腾讯御安全");
        reinforcementSoMaps.put("libtosprotection.armeabi.so", "腾讯御安全");
        reinforcementSoMaps.put("libtosprotection.x86.so", "腾讯御安全");
        reinforcementSoMaps.put("tosversion", "腾讯御安全");
        reinforcementSoMaps.put("libTmsdk-xxx-mfr.so", "腾讯御安全");
        reinforcementSoMaps.put("aliprotect.dat", "阿里加固");
        reinforcementSoMaps.put("libsgmain.so", "阿里加固");
        reinforcementSoMaps.put("libsgsecuritybody.so", "阿里加固");
        reinforcementSoMaps.put("libmobisec.so", "阿里加固");
        reinforcementSoMaps.put("libfakejni.so", "阿里加固");
        reinforcementSoMaps.put("libzuma.so", "阿里加固");
        reinforcementSoMaps.put("libzumadata.so", "阿里加固");
        reinforcementSoMaps.put("libpreverify1.so", "阿里加固");

        reinforcementSoMaps.put("kdpdata.so", "几维加固");
        reinforcementSoMaps.put("dex.dat", "几维加固");
        reinforcementSoMaps.put("libkdp.so", "几维加固");
        reinforcementSoMaps.put("libkwscmm.so", "几维加固");
        reinforcementSoMaps.put("libkwscr.so", "几维加固");
        reinforcementSoMaps.put("libkwslinker.so", "几维加固");

        reinforcementSoMaps.put("libexec.so", "爱加密");
        reinforcementSoMaps.put("libexecmain.so", "爱加密");
        reinforcementSoMaps.put("ijiami.dat", "爱加密");
        reinforcementSoMaps.put("ijiami.ajm", "爱加密");
        reinforcementSoMaps.put("af.bin", "爱加密");
        reinforcementSoMaps.put("signed.bin", "爱加密");

        reinforcementSoMaps.put("libchaosvmp.so", "娜迦");
        reinforcementSoMaps.put("libddog.so", "娜迦");
        reinforcementSoMaps.put("libfdog.so", "娜迦");
        reinforcementSoMaps.put("libedog.so", "娜迦");

        reinforcementSoMaps.put("libprotectClass.so", "360加固");
        reinforcementSoMaps.put("libjiagu.so", "360加固");
        reinforcementSoMaps.put("libjiagu_art.so", "360加固");
        reinforcementSoMaps.put("libjiagu_x86.so", "360加固");

       reinforcementSoMaps.put("libcmvmp.so", "中国移动安全加固");
       reinforcementSoMaps.put("libmogosec_dex.so", "中国移动安全加固");
       reinforcementSoMaps.put("libmogosec_sodecrypt.so", "中国移动安全加固");
      reinforcementSoMaps.put("libmogosecurity.so", "中国移动安全加固");

      reinforcementSoMaps.put("libbaiduprotect.so", "百度加固");
      reinforcementSoMaps.put("baiduprotect1.jar", "百度加固");
      reinforcementSoMaps.put("baiduprotect.jar", "百度加固");

       reinforcementSoMaps.put("libuusafe.jar.so", "UU安全加固");
       reinforcementSoMaps.put("libuusafe.so", "UU安全加固");
        reinforcementSoMaps.put("libuusafeempty.so", "UU安全加固");

        reinforcementSoMaps.put("dp.arm-v7.so.dat", "DexProtect加固");
        reinforcementSoMaps.put("dp.arm.so.dat", "DexProtect加固");

        reinforcementSoMaps.put("libegis.so", "通付盾加固");
        reinforcementSoMaps.put("libNSaferOnly.so", "通付盾加固");

        reinforcementSoMaps.put("libreincp.so", "珊瑚灵御加固");
        reinforcementSoMaps.put("libreincp_x86.so", "珊瑚灵御加固");

        reinforcementSoMaps.put("libnqshield.so", "网秦加固");

        reinforcementSoMaps.put("libnesec.so", "网易易盾");

        reinforcementSoMaps.put("libAPKProtect.so", "APKProtect加固");

        reinforcementSoMaps.put("libx3g.so", "顶象技术加固");

        reinforcementSoMaps.put("libitsec.so", "海云安加固");

        reinforcementSoMaps.put("libapssec.so", "盛大加固");

       reinforcementSoMaps.put("librsprotect.so", "瑞星加固");

        reinforcementSoMaps.put("libapktoolplus_jiagu.so", "apktoolplus加固");
   }

   public static void getReinforcementMothByPaths(String[] JavaSourceCodeDirs) throws IOException {
        for (String dirPath : JavaSourceCodeDirs) {
            File fl = new File(dirPath);
            if (fl.exists()) {
                getApkFileReinforcementMoth(fl);
            }
        }
   }

   public static void getReinforcementMothByPaths(ArrayList<String> JavaSourceCodeDirs) throws IOException {
        for (String dirPath : JavaSourceCodeDirs) {
            File fl = new File(dirPath);
            if (fl.exists()) {
                getApkFileReinforcementMoth(fl);
            }
        }
   }

   private static void getApkFileReinforcementMoth(File fl) throws IOException {
        if (fl != null && fl.exists()) {
            if (fl.isDirectory()) {
                File[] listFiles = fl.listFiles();
                for (File file : listFiles)
                    getApkFileReinforcementMoth(file);
            } else if (fl.isFile() && !fl.isHidden()) {
                if (fl.getName().endsWith(".apk")) {
                    Enumeration<? extends ZipEntry> entries = new ZipFile(fl).entries();
                    Set<String> libSo = new HashSet<String>();
                    while (entries.hasMoreElements()) {
                        ZipEntry apk = entries.nextElement();
                        if (!apk.isDirectory() && apk.getName().startsWith("lib/"))
                            libSo.add(apk.getName().substring(apk.getName().lastIndexOf("/") + 1));
                    }
                    if (libSo.isEmpty()) { // 没有lib
                        System.out.println(fl.getName() + "：\t从lib中没有分析出加固方式");
                        return;
                    }
                    Set<String> reinforcementMoth = new HashSet<String>();
                    // 有so
                    for (String libFile : libSo)
                        if (reinforcementSoMaps.containsKey(libFile)) {
                            reinforcementMoth.add(reinforcementSoMaps.get(libFile));
                        }
                    if (reinforcementMoth.size() > 0) {
                        System.out.println(fl.getName() + "：\t分析出加固方式为：" + reinforcementMoth.toString());
                    } else {
                        System.out.println(fl.getName() + "：\t从lib中没有分析出加固方式");
                    }
                    reinforcementMoth.clear();
                }
            }
        }
    }
}